package com.fc.redap.service.impl;

import com.fc.redap.dao.BaseDao;
import com.fc.redap.dao.UserDao;
import com.fc.redap.entity.UserEntity;
import com.fc.redap.service.IUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements IUserService {
	@Autowired
	private UserDao userDao;

	@Override
	public BaseDao getDao(){
		return userDao;
	}

	public UserEntity queryByUserName(String name){ return userDao.queryByUserName(name);}

	@Override
	public int queryTotal(Map<String, Object> map){
		return userDao.queryTotal(map);
	}
	
	@Override
	public void save(String mobile, String password){
		UserEntity user = new UserEntity();
		user.setPhone(mobile);
		user.setName(mobile);
		user.setPassword(DigestUtils.sha256Hex(password));
		user.setCreateTime(new Date());
		user.setStatus(1);
		userDao.save(user);
	}

	@Override
	public UserEntity queryByMobile(String mobile) {
		return userDao.queryByPhone(mobile);
	}

	@Override
	public int updatePassword(Long userId, String oldPwd, String newPwd){
		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		map.put("password", oldPwd);
		map.put("newPassword", newPwd);
		return userDao.updatePassword(map);
	}
}
